﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.DAL
{
    public class ProductDAL
    {
        public void UpdateProduct(ProductInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@SKU",SqlDbType.Int,4,info.SKU),
                db.MakeInParam("@Name",SqlDbType.VarChar,50,info.Name),
                db.MakeInParam("@Headline",SqlDbType.VarChar,50,info.Headline),
                db.MakeInParam("@PictureLocation", SqlDbType.VarChar, 100, info.PictureLocation),
                db.MakeInParam("@Price",SqlDbType.Float,8,info.Price),
                db.MakeInParam("@quantity",SqlDbType.Int,4,info.Quantity),
                db.MakeInParam("@Description",SqlDbType.VarChar,5000,info.Description),
                db.MakeInParam("@Technical",SqlDbType.VarChar,5000,info.Technical)
            };
            try
            {
                db.ExecNonQuery("CAM_Products_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        public DataSet GetAllProducts()
        {
            Database db = new Database();
            try
            {
                //db.ExecNonQuery("CAM_Products_GetAll");
                return db.GetDataSet("CAM_Products_GetAll");
                //return DataSet;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        public ProductInfo GetProductsBySKU(int SKU)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param = 
            {
                db.MakeInParam("@SKU", SqlDbType.Int, 4, SKU)
            };

            try
            {
                dr = db.RunProc("CAM_Products_GetBySKU", param);
                if (dr.Read())
                {
                    ProductInfo info = new ProductInfo();

                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.Description = DataHelper.GetString(dr["Description"]);
                    info.Headline = DataHelper.GetString(dr["Headline"]);
                    info.PictureLocation = DataHelper.GetString(dr["PictureLocation"]);
                    info.Name = DataHelper.GetString(dr["Name"]);
                    info.Price = DataHelper.GetDecimal(dr["Price"]);
                    info.Quantity = DataHelper.GetInt(dr["Quantity"]);
                    info.SKU = DataHelper.GetInt32(dr["SKU"]);
                    info.Status = DataHelper.GetInt32(dr["Status"]);
                    info.Technical = DataHelper.GetString(dr["Technical"]);
                    info.Void = DataHelper.GetInt32(dr["Void"]);

                    return info;
                }
                else
                {
                    return null;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        public int AddProduct(ProductInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@SKU",SqlDbType.Int,4,info.SKU),
                db.MakeInParam("@Name",SqlDbType.VarChar,50,info.Name),
                db.MakeInParam("@Headline",SqlDbType.VarChar,50,info.Headline),
                db.MakeInParam("@PictureLocation", SqlDbType.VarChar, 100, info.PictureLocation),
                db.MakeInParam("@Price",SqlDbType.Float,8,info.Price),
                db.MakeInParam("@quantity",SqlDbType.Int,4,info.Quantity),
                db.MakeInParam("@Description",SqlDbType.VarChar,5000,info.Description),
                db.MakeInParam("@Technical",SqlDbType.VarChar,5000,info.Technical)
            };
            try
            {
                db.ExecNonQuery("CAM_Products_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        public void DeleteProduct(int SKU)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@SKU",SqlDbType.Int,4,SKU)
            };
            try
            {
                db.ExecNonQuery("CAM_Products_Delete", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
    }
}
